import { request } from './client';
import { 
  BaseResponse, 
  UserInfo, 
  WalletInfo, 
  ScanValidationRequest, 
  ScanValidationResponse, 
  RentalRequest, 
  RentalResponse,
  DeviceInfo 
} from './types';
import { SW } from '../utils/baseUrl';


export async function loginAccount(payload: { code?: string; phone?: string; email?: string; msgCode: string; }): Promise<BaseResponse<UserInfo>> {
  return request('/api/login', { method: 'POST', body: payload });
}

export async function getWallet(token?: string): Promise<BaseResponse<WalletInfo>> {
  return request('/api/users/wallet', { method: 'GET', token });
}

// 扫码验证API
export async function validateScanCode(id: string, token?: string): Promise<BaseResponse<ScanValidationResponse>> {
  return request(`${SW}/app/r/scan.html/V2`, { method: 'GET', query: { id }, token });
}

// 获取设备详情API
export async function getDeviceDetail(deviceId: string, token?: string): Promise<BaseResponse<any>> {
  return request(`${SW}/app/rentbox/show.html/V2`, { method: 'GET', query: { deviceId }, token });
}

// 获取设备信息API
export async function getDeviceInfo(deviceId: string, token?: string): Promise<BaseResponse<DeviceInfo>> {
  return request(`/api/device/${deviceId}`, { method: 'GET', token });
}

// 创建租借订单API
export async function createRentalOrder(payload: RentalRequest, token?: string): Promise<BaseResponse<RentalResponse>> {
  return request('/api/rental/create', { method: 'POST', body: payload, token });
}

// 确认租借API
export async function confirmRental(orderId: string, token?: string): Promise<BaseResponse<{ success: boolean; message?: string }>> {
  return request(`/api/rental/${orderId}/confirm`, { method: 'POST', token });
}

// ZaloPay押金支付接口
export async function createZaloPayDeposit(payload: {
  deviceId?: string;
  powerBankType?: number;
}, token?: string): Promise<BaseResponse<string>> {
  return request(`${SW}/app/security/zalopay/V2`, { 
    method: 'POST', 
    body: payload, 
    token 
  });
}

// ZaloPay支付回调接口
export async function handleZaloPayCallback(payload: {
  data: string; // base64编码的订单数据
  mac: string;  // HMAC-SHA256签名
}, token?: string): Promise<BaseResponse<any>> {
  return request(`${SW}/app/security/pay/zalo/notify/V2`, { 
    method: 'POST', 
    body: payload, 
    token 
  });
}



